const passwordReg = /[\u4e00-\u9fa5]/;
const passwordMin = 3,
      passwordMax = 15;
// 验证规则对象
export const verifyObj = {
  password(rule, value, callback){
    if (value == undefined || value == null || value == '') {
      return callback(new Error('密码不能为空'))
    } else if (passwordReg.test(value)) {
      return callback(new Error('密码不能包含中文'))
    } else if (value.length < passwordMin || value.length > passwordMax) {
      return callback(new Error('密码长度不能小于3位且不能大于15位'))
    } else {
      callback()
    }
  },
  repeatPassword(pass){
    return function(rule, value, callback){
      if (value == undefined || value == null || value == ''){
        return callback(new Error('密码不能为空'))
      } else if (value != pass){
        return callback(new Error('两次输入密码不一致，请核对'))
      } else {
        callback()
      }
    }
  },
  chineseReg(rule, value, callback){
    if (value == undefined || value == null || value == '' || (typeof value === 'string' && !value.trim())){
      return callback(new Error('输入不能为空'));
    } else if (/[\u4e00-\u9fa5]/.test(value)){
      return callback(new Error('输入不能包含中文字符'));
    } else {
      callback();
    }
  }
}
// 创建必填项规则
export function createRequiredRule(message){
  return [ { required: true, message, trigger: ['blur', 'change'] } ];
  return [{ required: true, validator(rule, value, callback){
    if (value == undefined || value == null || value == '' || (typeof value === 'string' && !value.trim())){
      return callback(new Error(message));
    } else {
      callback();
    }
  }, trigger: ['blur', 'change']}]
}

// 用户名
export const username = [
  { required: true, message: '用户名不能为空', trigger: ['blur'] },
  { min: 5, max: 20, message: '长度范围不少于5字符，不超过20字符', trigger: ['blur', 'change'] }
];

// 注册的用户名
export const userName = [
  { required: true, message: '用户名不能为空', trigger: ['blur'] },
  { max: 30, message: '用户名不超过30字符', trigger: ['blur', 'change'] }
];

// 注册的用户名
export const nickName = [
  { required: true, message: '昵称不能为空', trigger: ['blur'] },
  { max: 30, message: '昵称不超过30字符', trigger: ['blur', 'change'] }
];

// 算法名称
export const moduleAlgo = [
  { required: true, message: '算法名称不能为空', trigger: ['blur', 'change'] },
  { max: 20, message: '算法名称不能超过20个字符', trigger: ['blur', 'change'] }
];

// 算法描述
export const algoDes = [
  { max: 100, message: '算法描述不能超过100个字符', trigger: ['blur', 'change'] }
]

// 算法负责人
export const owner = [
  { max: 10, message: '长度范围不超过10字符', trigger: ['blur', 'change'] }
]

// 密码
export const password = [
  { required: true, validator: verifyObj.password, trigger: ['blur', 'change'] }
];
